package com.zzgc.modules.controller;

import com.zzgc.common.entity.School;
import com.zzgc.common.entity.Student;
import com.zzgc.common.entity.StudentArchives;
import com.zzgc.common.response.Result;
import com.zzgc.modules.param.RefractometerParam;
import com.zzgc.modules.param.StudentArchivesParam;
import com.zzgc.modules.param.VisualParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 设备回调接口
 * @Author:
 * @CreateTime: 2022-12-03  14:08
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping("equipment")
@Api(tags = "检测设备数据回调")
public class EquipmentController extends BaseController{

    @PostMapping("optometer")
    @ApiOperation(value = "验光仪回调接口")
    public Map<String, Object> optometer(@RequestBody RefractometerParam refractometerParam){
        // {"status": 0}  0(失败)，1（成功）
        System.out.println("refractometerParam = " + refractometerParam);
        Map<String, Object> hashMap = new HashMap<>();
//        System.out.println("refractometerParam = " + refractometerParam);

        // 二维码 数据格式  区县代码_学生ID_次数
        //String s = "411621_7_1";
        //String[] s1 = refractometerParam.getQrcode().split("_");
        /* 修改 二维码换成学生唯一id */
        // 通过唯一id, 查询学号在本服务器的id
        Student student = new Student();
        // 唯一编码 加 年份查询
        student.setSStudentId(Long.valueOf(refractometerParam.getQrcode()));
        List<Student> studentList = studentService.queryByPage(student);
        // 判断是否存在这名学生
        //Student student = studentService.queryById(Long.valueOf(s1[1]));
        if (studentList.size() == 0){ // 不存在这名学生
            hashMap.put("status", false);
            return hashMap;
        }
        student = studentList.get(0);
        School school = schoolService.queryById(student.getSSchoolId());
        student.setTableName("archives_" + school.getSArea() + "_" + student.getSSchoolId());
        // 添加或修改数据
        StudentArchives archives = studentArchivesService.insertStudentArchives(getNumber(), student, refractometerParam);
        if (archives == null){
            hashMap.put("status", false);
            return hashMap;
        }

        // Qrcode=####, Id=00000000-4868-9f9d-0000-00000df2edc1, brand=XB, detection_day=22/12/03, detection_time=16:39:03,
        // ep_model=--, right_a_avg=119, left_a_avg=103, right_s_avg=-0.37, left_s_avg=-0.12, right_c_avg=-0.87, left_c_avg=-0.25,
        // right_avg_d=, left_avg_d=, right_avg_mm=, left_avg_mm=, right_kc=0.00, left_kc=0.00, right_r1_ax=, left_r1_ax=, right_r1_d=,
        // left_r1_d=, right_r1_mm=, left_r1_mm=, right_r2_ax=, left_r2_ax=, right_r2_d=, left_r2_d=, right_r2_mm=, left_r2_mm=,
        // pd=65.5, software_version=V1.0.3.220118

        hashMap.put("status", true);
        return hashMap;
    }

    @PostMapping("visual")
    @ApiOperation(value = "电子视力表回调接口")
    public Map<String, Object> visual(VisualParam visualParam){
        // visualParam = VisualParam(AppKey=tocersoft, AppSecret=tocersoft, userID=56756756, Right1=null, Right2=null, Left1=null, Left2=null, isGlasses=0)
        //visualParam = VisualParam(AppKey=tocersoft, AppSecret=tocersoft, userID=56756756, Right1=2.0, Right2=5.3, Left1=2.0, Left2=5.3, isGlasses=0)
        //visualParam = VisualParam(AppKey=tocersoft, AppSecret=tocersoft, userID=56756756, Right1=1.0, Right2=5.0, Left1=2.0, Left2=5.3, isGlasses=1)
//        System.out.println("visualParam = " + visualParam);
        HashMap<String, Object> hashMap = new HashMap<>();
        HashMap<String, Object> result = new HashMap<>();
        HashMap<String, Object> user = new HashMap<>();
        // 二维码 数据格式  区县代码_学生ID_次数
        //String s = "411621_7_1";
        //String[] s1 = visualParam.getUserID().split("_");
        // 查询学生
        // Student student = studentService.queryById(Long.valueOf(s1[1]));
        /* 修改 二维码换成学生唯一id */
        // 通过唯一id, 查询学号在本服务器的id
        Student student = new Student();
        // 唯一编码 加 年份查询
        student.setSStudentId(Long.valueOf(visualParam.getUserID()));
        List<Student> studentList = studentService.queryByPage(student);

        // 判断是否是用户查询
        if (visualParam.getRight1() == null && visualParam.getRight2() == null){
            // 判断是否存在这名学生
            if (studentList.size() == 0){ // 不存在这名学生
                result.put("status", 1);
                result.put("errmsg", "用户不存在");
                result.put("result", null);
                hashMap.put("json", result);
                return hashMap;
            }else {
                student = studentList.get(0);
                user.put("userName", student.getSName());
                user.put("userClass", student.getSClassName());
                result.put("status", 0);
                result.put("errmsg", "查询成功!");
                result.put("result", user);
                hashMap.put("json", result);
                return hashMap;
            }
        }
        student = studentList.get(0);
        School school = schoolService.queryById(student.getSSchoolId());
        student.setTableName("archives_" + school.getSArea() + "_" + student.getSSchoolId());

        StudentArchives archives = studentArchivesService.insertStudentArchives(getNumber(), student, visualParam);
        if (archives == null){
            result.put("status", 1);
            result.put("errmsg", "信息录入失败!");
            result.put("result", null);
            hashMap.put("json", result);
            return hashMap;
        }else {
            result.put("status", 0);
            result.put("errmsg", "录入成功!");
            result.put("result", null);
            hashMap.put("json", result);
            return hashMap;
        }
        //  status字段表示此次调用的成功状态。
        //0：成功  1:用户不存在（失败）
        //errmsg字段表示失败时的错误描述。
        //result字段为查询类接口的返回对象。
        // {
        //  "json":
        //   {
        //    "status":0,
        //    "errmsg":"",
        //    "result":  {"userName": “XXX”,”userClass”:”XXX” }
        //}
        //}
    }

}
